package 树;

public class 面试题731 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(0);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right = node4;
        面试题731 o = new 面试题731();
        System.out.println(o.sumNumbers(root));
    }
    /**
     *
     * @param root TreeNode类
     * @return int整型
     */
    public int sumNumbers (TreeNode root) {
        if(root == null) return 0;
        return help(root, (Integer) root.val);
    }

    private int help(TreeNode root, int sum) {
        if (root.left == null && root.right == null){
            return sum;
        }
        sum = sum * 10;
        int left = 0;
        int right = 0;
        int tem1 = sum;
        int tem2 = sum;
        if (root.left != null){
            tem1 += (int)root.left.val;
            left = help(root.left,tem1);
        }
        if (root.right != null){
            tem2 += (int)root.right.val;
            right = help(root.right,tem2);
        }
        return left + right;
    }
}
